package com.xbb.monitor.service;

import com.xbb.monitor.entity.Network;
import com.xbb.monitor.entity.echarts.LineBean;
import com.xbb.monitor.repository.NetworkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class NetworkService {
    @Autowired
    private NetworkRepository networkRepository;
    @PersistenceContext
    private EntityManager entityManager;

    public List<String> getNetworkName(){
        List<String> names = networkRepository.getName();
        return names;
    }

    public Map<String,Object> getData(Calendar calendar, int interval, SimpleDateFormat sdf,String networkName){
        String sql = "SELECT id,time, name, txresults, rxresults, rx_packets, tx_packets, tx_errors, rx_errors FROM (\n" +
                "  SELECT b.*,@rownum \\:= @rownum + 1 AS rownum,\n" +
                "  @ptime2 \\:=b.time as ptime2,\n" +
                "  if(TIMESTAMPDIFF(MINUTE,@ptime,b.time)<?2,@ptime=null,@ptime\\:=@ptime2) AS ptime FROM\n" +
                "(SELECT * FROM t_network WHERE TIME >= ?1 and name = ?3  ORDER BY time) b,\n" +
                "(SELECT @rownum \\:= 0,@ptime\\:= NULL ,@rank\\:=0,@ptime2\\:=NULL ) a\n" +
                ") AS c WHERE ptime IS NOT NULL";

        Query query = entityManager.createNativeQuery(sql,Network.class);
        query.setParameter(1,calendar.getTime());
        query.setParameter(2,interval);
        query.setParameter(3,networkName);
        List<Network> networks = query.getResultList();

        Map<String,Object> map = new HashMap<>();
        List<LineBean> lineBeansResult = new ArrayList<LineBean>();
        List<LineBean> lineBeansAll = new ArrayList<LineBean>();

        LineBean tx = new LineBean("tx","line");
        LineBean rx = new LineBean("rx","line");
        LineBean txPacket = new LineBean("txPacket","line");
        LineBean rxPacket = new LineBean("rxPacket","line");
        LineBean txError = new LineBean("txError","line");
        LineBean rxError = new LineBean("rxError","line");

        List<String[]> datasTx = new ArrayList<String[]>();
        List<String[]> datasRx = new ArrayList<String[]>();
        List<String[]> datasTxPackets = new ArrayList<String[]>();
        List<String[]> datasRxPackets = new ArrayList<String[]>();
        List<String[]> datasTxErrors = new ArrayList<String[]>();
        List<String[]> datasRxErrors = new ArrayList<String[]>();

        for(Network network : networks){
            String[] dataTx = {sdf.format(network.getTime()),String.format("%.2f",Double.parseDouble(network.getTxresults())/1024)};
            String[] dataRx = {sdf.format(network.getTime()),String.format("%.2f",Double.parseDouble(network.getRxresults())/1024)};
            String[] dataTxPacket = {sdf.format(network.getTime()),network.getTxPackets()};
            String[] dataRxPacket = {sdf.format(network.getTime()),network.getRxPackets()};
            String[] dataTxError = {sdf.format(network.getTime()),network.getTxErrors()};
            String[] dataRxError = {sdf.format(network.getTime()),network.getRxErrors()};

            datasTx.add(dataTx);
            datasRx.add(dataRx);
            datasTxPackets.add(dataTxPacket);
            datasRxPackets.add(dataRxPacket);
            datasTxErrors.add(dataTxError);
            datasRxErrors.add(dataRxError);
        }

        tx.setData(datasTx);
        rx.setData(datasRx);
        txPacket.setData(datasTxPackets);
        rxPacket.setData(datasRxPackets);
        txError.setData(datasTxErrors);
        rxError.setData(datasRxErrors);

        lineBeansResult.add(tx);
        lineBeansResult.add(rx);
        lineBeansAll.add(txPacket);
        lineBeansAll.add(rxPacket);
        lineBeansAll.add(txError);
        lineBeansAll.add(rxError);

        map.put("results",lineBeansResult);
        map.put("all",lineBeansAll);

        return map;

    }

    public static void main(String[] args) {
        String a = "0";
        System.out.println(String.format("%.2f",Double.parseDouble(a)/1024));
    }
}
